package com.lingxu.base.system.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.common.util.DateUtil;
import com.lingxu.base.system.api.ISysBaseAPI;
import com.lingxu.base.system.common.entity.SysLog;
import com.lingxu.base.system.common.entity.SysLogOption;
import com.lingxu.base.system.common.entity.SysLogSearchVO;
import com.lingxu.base.system.common.service.ISysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Description: 日志信息
 * @Author: 朱家池
 * @Date:   2021-08-04
 * @Version: V1.0.1
 */
@Slf4j
@Api(tags="系统日志信息")
@RestController
@RequestMapping("/system/sysLog")
public class SysLogController{
	@Resource
	private ISysLogService sysLogService;
	@Resource
	private ISysBaseAPI sysBaseAPI;

	 @ApiOperation(value="日志管理-分页查询日志", notes="日志管理-分页查询日志")
	 @PostMapping(value = "/getPageList")
	 public Result<?> getPageList(@RequestBody SysLogOption sysLog) {
		 Page<SysLog> page = new Page<SysLog>(sysLog.getPageNo(), sysLog.getPageSize());
		 IPage<SysLog> result=sysLogService.getPageList(page,sysLog);
		 return Result.ok(result);
	 }


	@ApiOperation(value="平台日志-分页查询平台日志日志", notes="平台日志-分页查询平台日志日志")
	@PostMapping(value = "/getPTLogPageList")
	public Result<?> getPTLogPageList(@RequestBody SysLogSearchVO vo) {
		Page<SysLog> page = new Page<SysLog>(vo.getPageNo(), vo.getPageSize());
		IPage<SysLog> result=sysLogService.getPTLogPageList(page,vo);
		return Result.ok(result);
	}

	 /**
	  * @Author ysj闫世杰
	  * @Description 导出pdf
	  * @Date 2023/6/19 11:28
	  * @Param: startTime
 	  * @Param: endTime
	  * @return void
	  **/
	 @RequestMapping(value = "/downLogFilePDF")
	 public void downLogFilePDF(SysLog sysLog,String startTime, String endTime, HttpServletRequest request, HttpServletResponse response){
	 	String conetnt = "用户名: 【" + sysLog.getRealname() + "】,在【" + DateUtil.getDateTime() + "】导出日志文件！";
	 	sysLog.setCreatetime(new Date());
	 	sysLog.setCreateby(sysLog.getUserid());
	 	sysLog.setLogtype(1);
	 	sysLog.setIp(request.getRemoteAddr());
	 	sysLog.setLogcontent(conetnt);
	 	sysLogService.save(sysLog);
	 	sysLogService.downLogFilePDF(sysLog.getRealname(),startTime,endTime,request,response);
	 }

	/**
	 * @Author ysj闫世杰
	 * @Description 查询备份日志
	 * @Date 2023/6/26 17:14
	 * @Param: dateTime
	 * @return com.lingxu.base.common.api.vo.Result
	 **/
	 @RequestMapping(value = "/findBackUpLogs")
	 public Result findBackUpLogs(Integer limit,Integer page,String dateTime){
	 	return sysLogService.findBackUpLogs(limit, page, dateTime);
	 }

	 /**
	  * @Author ysj闫世杰
	  * @Description 手动点击全量备份数据库
	  * @Date 2023/6/27 10:08
	  * @Param:
	  * @return com.lingxu.base.common.api.vo.Result
	  **/
	 @RequestMapping(value = "/createBackUpFile")
	 public Result createBackUpFile(){
	 	return sysLogService.createBackUpFile();
	 }
 }
